<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/tag/taglibs.jsp" %>

// 页面加载初始化方法
$(function () {
    initDataGrid();
});
// 数据表格初始化方法
function initDataGrid() {
    $('#displayTable').datagrid({
        fit:true, //表格自适应
        fitColumns:true, //列自适应
        nowrap:true, //是否换行
        autoRowHeight:false, //自动行高
        striped:true,
        collapsible:true, //是否可折叠
        url:'<c:url value="/system/queryUsers.do" />',
        sortName:'loginId',
        sortOrder:'asc',
        remoteSort:false,
        idField:'loginId',
        singleSelect:false, //是否单选
        pagination:true, //分页控件
        rownumbers:true, //行号
        scrollbarSize:0,
        loadMsg:"数据加载中.....",
        rowStyler:function (index, row) {
            if (row.enabled == 0) {
                return 'color:#748494;';
            }
        },
        frozenColumns:[
            [
                {field:'ck', checkbox:true},
                {title:'登录ID', field:'loginId', width:50, sortable:true}
            ]
        ],
        columns:[
            [
                {title:'基础信息', colspan:13},
                {field:'userId', title:'操作', width:30, align:'center', rowspan:2,
                    formatter:function (value, row, index) {
                        var optStr = '<a href="#" style="color:red;text-decoration:none" onclick="updateUser(\'' + value + '\');">';
                        optStr += '<img src="<c:url value="/images/admin/45.png" />" width="16" height="16" border="0" alt="修改">';
                        optStr += '</a>';
                        optStr += '&nbsp;&nbsp;&nbsp;&nbsp;';
                        // searchUserRoles
                        if (row.enabled == 1) {
                            optStr += '<a href="#" style="color:red;text-decoration:none;" onclick="editUserRoles(\'' + value + '\',\'' + row.loginId + '\',\'' + row.name + '\');">';
                            optStr += '<img src="<c:url value="/images/admin/42.png" />" width="16" height="16" border="0" alt="授权">';
                            optStr += '</a>';
                        } else {
                            optStr += '<img src="<c:url value="/images/admin/42-1.png" />" style="" width="16" height="16" border="0" />';
                        }
                        return optStr;
                    }
                }
            ],
            [
                {field:'name', title:'姓名', width:40,
                    formatter:function (value, row, index) {
                        return '<a href="#" style="text-decoration:none;" ' +
                            'onclick="displayUser(\'' + row.userId + '\');">' + row.name + '</a>';
                    }
                },
                {field:'gender', hidden:true},
                {field:'genderName', title:'性别', width:20},
                {field:'birthday', title:'生日', width:30, sortable:true},
                {field:'deptId', hidden:true},
                {field:'deptName', title:'部门', width:60},
                {field:'job', hidden:true},
                {field:'jobName', title:'职位', width:50},
                {field:'mobile', title:'手机', width:32},
                {field:'tel', title:'电话', width:30},
                {field:'email', title:'邮箱', width:100},
                {field:'enabled', hidden:true},
                {field:'enabledName', title:'有效性', width:24}

            ]
        ],
        toolbar:"#dataGridToolbar"
    });
    //设置分页控件
    var p = $('#displayTable').datagrid('getPager');
    $(p).pagination({
        pageSize:10, //每页显示的记录条数，默认为10
        pageList:[10, 20, 30, 40, 50], //可以设置每页记录条数的列表
        beforePageText:'第', //页数文本框前显示的汉字
        afterPageText:'页    共 {pages} 页',
        displayMsg:'当前显示 {from} - {to} 条记录   共 {total} 条记录'
    });
}
// 窗口初始化方法
function initWindow(title, url) {
    $('#userDialog').window({
        title:title,
        href:url
    });
}
// 查看用户详细信息
function displayUser(value) {
    var title = '详细用户信息';
    var url = '<c:url value="/system/queryUserInfo.do?userQuery.userId=" />' + value;
    initWindow(title, url);
    $('#userDialog').window('refresh');
    $('#userDialog').window('open');
}
// 添加新用户
function insertUser() {
    var title = '添加用户信息';
    var url = '<c:url value="/system/user/user_edit.jsp" />';
    initWindow(title, url);
    $('#userDialog').window('refresh');
    $('#userDialog').window('open');
}
// 更新用户信息
function updateUser(value) {
    var title = '修改用户信息';
    var url = '<c:url value="/system/queryUser.do?userQuery.userId=" />' + value;
    initWindow(title, url);
    $('#userDialog').window('refresh');
    $('#userDialog').window('open');
}
// 逻辑删除[更新用户为无效状态]
function deleteUser() {
    var ids = [];
    var rows = $('#displayTable').datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].userId);
    }
    $("#deleteUserId").val(ids.join(':'));
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].loginId);
    }
    $("#deleteLoginId").val(ids.join(':'));
    var url = '<c:url value="/system/deleteUser.do" />';
    $('#deleteForm').form('submit', {
        url:url,
        success:function (data) {
            var data = eval('(' + data + ')');
            if (data.success) {
                alert(data.message)
                doSearch('reload');
            }
        }
    });
}
// 添加或更新用户表单提交方法
function doSubmit(maker) {
    var url = '<c:url value="/system/updateUser.do" />';
    if (maker == 'insert') {
        url = '<c:url value="/system/insertUser.do" />';
    }
    $('#userItemForm').form('submit', {
        url:url,
        success:function (data) {
            var data = eval('(' + data + ')');
            if (data.success) {
                alert(data.message)
                doCancel();
                doSearch('reload');
            }
        }
    });
}
// 关闭表单层方法
function doCancel() {
    $('#userDialog').window('close');
}
// 用户信息查询方法
function doSearch(action) {
    $('#displayTable').datagrid(action, {
        'userQuery.loginId':$('#loginId').val(),
        'userQuery.name':$('#name').val(),
        'userQuery.deptId':$('#deptId').combobox('getValue'),
        'userQuery.job':$('#job').combobox('getValue'),
        'userQuery.email':$('#email').val(),
        'userQuery.mobile':$('#mobile').val(),
        'userQuery.enabled':$('#enabled').combobox('getValue')
    });
}